####################################################################################

source ~/.bashrc
source ~/20220915_gastric_multiple/dna_combinePublic/config/config.sh

####################################################################################
## 生成对应文件列表
## bam文件放到一个目录下
mkdir -p ${work_dir}/public_data/wes/fastq
mkdir -p ${work_dir}/public_data/wes/bam
mkdir -p ${work_dir}/public_data/wes/vcf
mkdir -p ${work_dir}/public_data/wes/maf
mkdir -p ${work_dir}/public_data/wes/seg
mkdir -p ${work_dir}/public_data/wes/chat

## fastq文件目录
export fastq_link_path=${work_dir}/public_data/wes/fastq
## bam文件目录
export bam_link_path=${work_dir}/public_data/wes/bam
## vcf文件目录
export vcf_link_path=${work_dir}/public_data/wes/vcf
## maf文件目录
export maf_link_path=${work_dir}/public_data/wes/maf
## seg文件目录
export seg_link_path=${work_dir}/public_data/wes/seg
## chat文件目录
export chat_link_path=${work_dir}/public_data/wes/chat

## trim以后的fastq
find \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch1/results/Trim_fastq/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch2/results/Trim_fastq/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch_raw/trim_fastq/ | grep _trim.fq.gz \
> ${work_dir}/public_data/wes/trim_fq.list

## bam文件
find /public/home/xxf2019/20220915_gastric_multiple/dna_batch1/results/bam/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch2/results/bam/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch_raw/results/bam/ | grep recal \
> ${work_dir}/public_data/wes/bam_file.list

## 文件头行
echo "OriginalID,Project,Access,Version,PrimarySite,Disease,Biospecimen,platform,Cases,Data_Category,Data_Format,File_Name,File_Sizes,SampleID,IndividualID" \
> ${work_dir}/public_data/NJGC_Somatic.datalist.csv

####################################################################################
Project=NJGC_Somatic
Version=20220915
PrimarySite=Stomach
Disease=STAD
platform="Illumina(DNA)"
Cases=1

####################################################################################
## level1数据整理
Access=Level1

for Tumor in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk '{OFS="\t"}{print $3}' | sort -u`
do

###########################################
## 图上对应的ID
normal=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $2}' | sort -u`
plotid=`cat ${config_path}/plotID.list | grep -w ${normal} | awk -F'\t' '{print $4}'`
sample_order=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $5}'`
IndividualID=NJ${plotid}
type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk -F'\t' '{print $6}' | sort -u | sed 's/ //g'`

###########################################
## 判断生物样本类型
bio_type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $4}'`
if [[ $bio_type == "IM" ]]
then
Biospecimen="Intestinal Metaplasia"
plus=`echo $sample_order | awk -F'-' '{print ($2-1)}'`
SampleID=${IndividualID}-01${plus}
else
Biospecimen="Stomach Cancer Tissue"
plus=`echo $sample_order | awk -F'-' '{print $2}'`
SampleID=${IndividualID}-00${plus}
if [[ $type == "IM+IGC+DGC" && ${bio_type} == "DGC" ]]
then
igc_num=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk '{print $4}' | grep -w "IGC" | wc -l`
plus=`echo $sample_order | awk -F'-' '{print $2+igc_num}' igc_num=${igc_num}`
SampleID=${IndividualID}-00${plus}
fi
if [[ $Tumor == "S35" ]]
then
SampleID=${IndividualID}-001
fi
fi


###########################################
## 数据整理
## fastq文件
Data_Category="Trimmed Sequencing Reads"
Data_Format="FASTQ"
## 分R1和R2
trim_type=( "R1"  "R2" )
for type in ${trim_type[@]}
do
File_Name=${Tumor}_${type}_trim.fq.gz
raw_file=`cat ${work_dir}/public_data/wes/trim_fq.list | grep "/${File_Name}"`
File_Name_new=${SampleID}_${type}_trim.fq.gz

## 文件大小
File_Size=`ll -h ${raw_file} | awk '{print $5}'`
ln -snf ${raw_file} ${fastq_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv
done

done

###########################################
## 正常血样
for Tumor in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk '{OFS="\t"}{print $2}' | sort -u`
do

###########################################
## 图上对应的ID
normal=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $2}' | sort -u`
plotid=`cat ${config_path}/plotID.list | grep -w ${normal} | awk -F'\t' '{print $4}'`
sample_order=Blood
IndividualID=NJ${plotid}

###########################################
## 判断生物样本类型
Biospecimen="Blood"
SampleID=${IndividualID}-020

###########################################
## 数据整理
## fastq文件
Data_Category="Trimmed Sequencing Reads"
Data_Format="FASTQ"
## 分R1和R2
trim_type=( "R1"  "R2" )
for type in ${trim_type[@]}
do
File_Name=${Tumor}_${type}_trim.fq.gz
raw_file=`cat ${work_dir}/public_data/wes/trim_fq.list | grep "/${File_Name}"`
File_Name_new=${SampleID}_${type}_trim.fq.gz

## 文件大小
File_Size=`ll -h ${raw_file} | awk '{print $5}'`
ln -snf ${raw_file} ${fastq_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv
done
done

####################################################################################
## level2数据整理
## bam数据整理
Access=Level2

for Tumor in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk '{OFS="\t"}{print $3}' | sort -u`
do

###########################################
## 图上对应的ID
normal=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $2}' | sort -u`
plotid=`cat ${config_path}/plotID.list | grep -w ${normal} | awk -F'\t' '{print $4}'`
sample_order=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $5}'`
IndividualID=NJ${plotid}
type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk -F'\t' '{print $6}' | sort -u | sed 's/ //g'`

###########################################
## 判断生物样本类型
bio_type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $4}'`
if [[ $bio_type == "IM" ]]
then
Biospecimen="Intestinal Metaplasia"
plus=`echo $sample_order | awk -F'-' '{print ($2-1)}'`
SampleID=${IndividualID}-01${plus}
else
Biospecimen="Stomach Cancer Tissue"
plus=`echo $sample_order | awk -F'-' '{print $2}'`
SampleID=${IndividualID}-00${plus}
if [[ $type == "IM+IGC+DGC" && ${bio_type} == "DGC" ]]
then
igc_num=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk '{print $4}' | grep -w "IGC" | wc -l`
plus=`echo $sample_order | awk -F'-' '{print $2+igc_num}' igc_num=${igc_num}`
SampleID=${IndividualID}-00${plus}
fi
if [[ $Tumor == "S35" ]]
then
SampleID=${IndividualID}-001
fi
fi

###########################################
## 数据整理
## bam文件
Data_Category="Alignment Sequencing Reads"
Data_Format="BAM"
File_Name=${Tumor}-recal.bam
File_Name_new=${SampleID}-recal.bam

raw_file=`cat ${work_dir}/public_data/wes/bam_file.list | grep -w ${File_Name} | grep -v bai`
File_Size=`ll -h ${raw_file} | awk '{print $5}'`
ln -snf ${raw_file} ${bam_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv
done


## 正常组织
for Tumor in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk '{OFS="\t"}{print $2}' | sort -u`
do

###########################################
## 图上对应的ID
normal=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $2}' | sort -u`
plotid=`cat ${config_path}/plotID.list | grep -w ${normal} | awk -F'\t' '{print $4}'`
sample_order=Blood
IndividualID=NJ${plotid}

###########################################
## 判断生物样本类型
Biospecimen="Blood"
SampleID=${IndividualID}-020

###########################################
## 数据整理
## bam文件
Data_Category="Alignment Sequencing Reads"
Data_Format="BAM"
File_Name=${Tumor}-recal.bam
File_Name_new=${SampleID}-recal.bam

raw_file=`cat ${work_dir}/public_data/wes/bam_file.list | grep -w ${File_Name}| grep -v bai`
File_Size=`ll -h ${raw_file} | awk '{print $5}'`
ln -snf ${raw_file} ${bam_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv
done


####################################################################################
## level3数据整理
Access=Level3

for line in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk '{OFS=","}{print $3,$2}' | sort -u`
do

Tumor=`echo ${line} | awk -F',' '{print $1}'`
Normal=`echo ${line} | awk -F',' '{print $2}'`

###########################################
plotid=`cat ${config_path}/plotID.list | grep -w ${Normal} | awk -F'\t' '{print $4}'`
sample_order=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $5}'`
IndividualID=${plotid}
type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk -F'\t' '{print $6}' | sort -u | sed 's/ //g'`

###########################################
## 判断生物样本类型
bio_type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $4}'`
if [[ $bio_type == "IM" ]]
then
Biospecimen="Intestinal Metaplasia"
plus=`echo $sample_order | awk -F'-' '{print ($2-1)}'`
SampleID=${IndividualID}-01${plus}
else
Biospecimen="Stomach Cancer Tissue"
plus=`echo $sample_order | awk -F'-' '{print $2}'`
SampleID=${IndividualID}-00${plus}
if [[ $type == "IM+IGC+DGC" && ${bio_type} == "DGC" ]]
then
igc_num=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk '{print $4}' | grep -w "IGC" | wc -l`
plus=`echo $sample_order | awk -F'-' '{print $2+igc_num}' igc_num=${igc_num}`
SampleID=${IndividualID}-00${plus}
fi
if [[ $Tumor == "S35" ]]
then
SampleID=${IndividualID}-001
fi
fi

###########################################
## 数据整理
## vcf文件
Data_Category="Simple Nucleotide Variation"
Data_Format="VCF"
File_Name=${Tumor}_${Normal}_GGA_Filter.vcf
File_Size=`ll -h ${vcf_path}/${File_Name} | awk '{print $5}'`
File_Name_new=${SampleID}.vcf

cat ${vcf_path}/${File_Name} | grep -v "##" > ${vcf_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv

###########################################
## 数据整理
## maf文件
Data_Category="Annotation Simple Nucleotide Variation"
Data_Format="MAF"
File_Name=${Tumor}_${Normal}_GGA_Filter_funcotated.maf
File_Size=`ll -h ${maf_path}/${File_Name} | awk '{print $5}'`
File_Name_new=${SampleID}.maf

cat ${maf_path}/${File_Name} | grep Hugo_Symbol > ${maf_link_path}/${File_Name_new}
cat ${maf_path}/${File_Name} | grep -v -E "##|#version" |\
awk -F'\t' '{OFS="\t"}{$16=id;print}' id=${plotid}_${sample_order} >> ${maf_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv

###########################################
## 数据整理
## CNV文件
Data_Category="Allele-specific copy number"
Data_Format="SEG"
File_Name=${Tumor}_${Normal}.seg
File_Size=`ll -h ${Titan_path}/FinalModel/${File_Name} | awk '{print $5}'`
File_Name_new=${SampleID}.seg

## 修改样本名
cat ${Titan_path}/FinalModel/${File_Name} | head -1 > ${seg_link_path}/${File_Name_new}
cat ${Titan_path}/FinalModel/${File_Name} | sed '1d' |\
awk -F'\t' '{OFS="\t"}{$1=id;if($2!="X")print}' id=${plotid}_${sample_order} >> ${seg_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv

###########################################
## 数据整理
## 突变时期
Data_Category="Clonal classification of mutations"
Data_Format="TSV"
File_Name=${Tumor}_CHAT.txt
File_Size=`ll -h ${Titan_path}/chat/${File_Name} | awk '{print $5}'`
File_Name_new=${SampleID}_CHAT.txt

cp -rf ${Titan_path}/chat/${File_Name} ${chat_link_path}/${File_Name_new}

echo ${Tumor}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.csv

done

